define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'usermanage/users/index' + location.search,
                    add_url: 'usermanage/users/add',
                    edit_url: 'usermanage/users/edit',
                    del_url: 'usermanage/users/del',
                    multi_url: 'usermanage/users/multi',
                    import_url: 'usermanage/users/import',
                    table: 'usermanage/users',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'account', title: __('Account'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'realname', title: __('Realname'), operate: 'LIKE'},
                        {field: 'createtime', title: __('加入时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'parent_name', title: __('推荐人'), operate: false},
                        {field: 'contact_name', title: __('接点人'), operate: false},
                        {field: 'is_distributor', title: __('激活状态'), formatter: Table.api.formatter.normal, searchList: { 0: __('未激活'), 1: __('已激活')}},
                        {field: 'total_price', title: __('累计奖金')},
                        {field: 'price', title: __('剩余奖金')},
                        {field: 'group_name', title: __('分组名称'), operate: false},
                        {
                            field: 'operate', title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons:[
                                {
                                    name: 'detail',
                                    text: __('用户关系'),
                                    icon: '',
                                    refresh:true,
                                    classname: 'btn btn-info btn-xs',
                                    url: 'usermanage/relation/index?user_id={id}',
                                   /* name: 'money',
                                    text: __('用户关系'),
                                    classname: 'btn btn-primary btn-xs btn-detail btn-dialog',
                                    url: 'usermanage/relation/index?user_id={id}',*/
                                },
                                {
                                    name: 'click',
                                    title: __('点击执行事件'),
                                    text: '充值积分',
                                    classname: 'btn btn-xs btn-info btn-click',
                                    // icon: 'fa fa-leaf',
                                    // dropdown: '更多',//如果包含dropdown，将会以下拉列表的形式展示
                                    click: function (e, row) {
                                        Layer.prompt({
                                            title: "充值积分",
                                            success: function (layero) {
                                                $("input", layero).prop("placeholder", "填写充值积分");
                                            }
                                        }, function (value) {
                                            Fast.api.ajax({
                                                url: "usermanage/users/rechargeintegral/ids/" + row.id,
                                                data: {integral: value},
                                            }, function (data, ret) {
                                                Layer.closeAll();
                                                $(".btn-refresh").trigger("click");
//return false;
                                            });
                                        });
                                        return false;
                                    }
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'usermanage/users/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'usermanage/wage/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'usermanage/wage/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        childlist: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'usermanage/users/childlist' + location.search,
                },
                queryParams: function (params) { //自定义搜索条件
                    var filter = params.filter ? JSON.parse(params.filter) : {}; //判断当前是否还有其他高级搜索栏的条件
                    var op = params.op ? JSON.parse(params.op) : {};  //并将搜索过滤器 转为对象方便我们追加条件
                    filter.parent_id= Config.parent_id;     //将透传的参数 Config.group_id，追加到搜索条件中
                    op.parent_id= "=";  //group_id的操作方法的为 找到相等的
                    params.filter = JSON.stringify(filter); //将搜索过滤器和操作方法 都转为JSON字符串
                    params.op = JSON.stringify(op);
                    //如果希望忽略搜索栏搜索条件,可使用
                    //params.filter = JSON.stringify({url: 'login'});
                    //params.op = JSON.stringify({url: 'like'});
                    return params;

                },
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'usermanage/users/childlist' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'realname', title: __('姓名'), align: 'left'},
                        {field: 'avatar_url', title: __('头像'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
